package com.ciei.dpagm.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ciei.dpagm.common.service.BaseService;
import com.ciei.dpagm.entity.RuleSetParameterModule;
import com.ciei.dpagm.mapper.RuleSetParameterModuleMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pp
 * @date 2023/04/06
 */
@Service
public class RuleSetParameterModuleService extends BaseService<RuleSetParameterModuleMapper, RuleSetParameterModule> {

    /**
     * 根据规则集id列表获取规则集变量模块列表
     * @param ruleSetIdList
     * @return
     */
    public List<RuleSetParameterModule> getListByRuleSetIdList(List<Integer> ruleSetIdList){
        if(ruleSetIdList.isEmpty()){
            return new ArrayList<>();
        }
        LambdaQueryWrapper<RuleSetParameterModule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RuleSetParameterModule::getRuleSetId,ruleSetIdList);
        return list(lambdaQueryWrapper);
    }

    /**
     * 根据规则集id列表删除
     * @param ruleSetIdList
     */
    public void removeByRuleSetIdList(List<Integer> ruleSetIdList){
        if(ruleSetIdList.isEmpty()){
            return;
        }
        LambdaQueryWrapper<RuleSetParameterModule> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(RuleSetParameterModule::getRuleSetId,ruleSetIdList);
        remove(lambdaQueryWrapper);
    }

    /**
     * 模块是否被引用
     * @param parameterModuleIds
     * @return
     */
    public Boolean isReferenced(List<Integer> parameterModuleIds){
        if (parameterModuleIds != null && !parameterModuleIds.isEmpty()){
            QueryWrapper<RuleSetParameterModule> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(RuleSetParameterModule::getParameterModuleId,parameterModuleIds);
            int count = count(queryWrapper);
            if (count > 0){
                return true;
            }
        }
        return false;
    }
}
